'use strict';
require('./index.css')
require('page/common/nav/index.js')
require('page/common/header/index.js')
var _mm = require('util/mm.js')
var _order = require('service/order-service.js')
var navSide = require('page/common/nav-side/index.js')
var template = require('./index.string')

var page = {
    data: {
        orderNumber : _mm.getUrlParam('orderNumber')
    },
    init: function () {
        this.onLoad();
        this.bindEvent();
    },
    onLoad: function () {
        //初始化左侧菜单
        navSide.init({
            name: 'order-list'
        });
        // 加载detail
        this.loadDetail ()
    },
    loadDetail : function () {
        var _this = this,
            orderDetailHtml = '',
            $content = $('.content');
        $content.html('<div class="loading"></div>')
        _order.getOrderDetail(this.data.orderNumber, function (res) {
            _this.dataFilter(res);
            orderDetailHtml = _mm.renderHtml(template, res);
            $content.html(orderDetailHtml)
        }, function (msg) {  
            $content.html('<div class="err-tip">'+ msg +'</div>')
        })
    },
 
    bindEvent : function(){
        var _this = this;
        $(document).on('click','.order-cancel',function(){
            if(window.confirm('确定要取消该订单吗?')){
                _order.cancelOrder(_this.data.orderNumber,function(){
                    _mm.successTips('订单取消成功')
                    _this.loadDetail();
                },function(errMsg){
                    _mm.errorTios(errMsg)
                })
            }
        })
    },
    dataFilter : function(res){
        res.needPay      = res.status == 10 ;
        res.isCancelable = res.status == 10 ;
    }



}

$(function () {
    page.init()
})